// 获取安全的随机整数
function getSecureRandomInt(max:number) {
    const array = new Uint32Array(1);
    crypto.getRandomValues(array);
    return array[0] % max;
}
/**

*/
export const uuid = function (len = 16, radix?:number) {
    const chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
    const uuid:string[] =  [];
    radix = radix || chars.length;

    if (len) {
        // 生成指定长度的随机字符串
        for (let i = 0; i < len; i++) {
            uuid[i] = chars[getSecureRandomInt(radix)];
        }
    } else {
        let r;
        uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
        uuid[14] = '4';
        for (let i = 0; i < 36; i++) {
            if (!uuid[i]) {
                r = getSecureRandomInt(16);
                uuid[i] = chars[i === 19 ? (r & 0x3) | 0x8 : r];
            }
        }
    }

    return uuid.join('');
};